//
//  YCExamBeforeViewController.m
//  YCSchool
//
//  Created by yanglinxia on 16/4/26.
//  Copyright © 2016年 yanglinxia. All rights reserved.
//  考前学习

#import "YCExamBeforeViewController.h"
#import "YCExamBeforeTitleButton.h"

#import "YCCourseOneViewController.h"
#import "YCCourseTwoViewController.h"
#import "YCCourseThreeViewController.h"
#import "YCCourseFourViewController.h"

@interface YCExamBeforeViewController ()<UIScrollViewDelegate>

/** scrollView */
@property (nonatomic, weak) UIScrollView *scrollView;
/** titlesView */
@property (nonatomic, weak) UIView *titlesView;
/** 下划线 */
@property(strong, nonatomic) UIView *underline;
/** 选中的按钮 */
@property (nonatomic, strong) YCExamBeforeTitleButton *currentSelectedBtn;

@end

@implementation YCExamBeforeViewController


- (void)viewDidLoad {
    
    [super viewDidLoad];
    [self setUpNavgationBar];
    [self setUpScrollView];
    [self setUpTitlesView];
    [self setUpChildVcs];
    [self addChildVcViewToScrollView:0];
    [self resloveSilderSlipProblem];
}
- (void)resloveSilderSlipProblem
{
    NSArray *gestureArray = self.navigationController.view.gestureRecognizers;
    for (UIGestureRecognizer *gesture in gestureArray) {
        if ([gesture isKindOfClass:[UIScreenEdgePanGestureRecognizer class]]) {
            [self.scrollView.panGestureRecognizer requireGestureRecognizerToFail:gesture];
        }
    }
}

#pragma mark - 初始化
- (void)setUpNavgationBar
{
    self.navigationItem.title = @"考前学习";
    [self yc_addBackBarButtonItem];
}
/**
 *  添加内容滚动的scrollView
 */
- (void)setUpScrollView
{
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    scrollView.frame = CGRectMake(0, 0, self.view.yc_width, self.view.yc_height);
    [self.view addSubview:scrollView];
    scrollView.delegate = self;
    scrollView.pagingEnabled = YES;
    scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView = scrollView;
    
}
/**
 *  添加标题栏
 */
- (void)setUpTitlesView
{
    UIView *titlesView = [[UIView alloc] init];
    titlesView.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:1];
    titlesView.frame = CGRectMake(0, 0, self.view.yc_width, 49);
    [self.view addSubview:titlesView];
    _titlesView = titlesView;
    [self addTitlesBtn];
    [self addUnderLine];
}

/**
 *  添加标题栏中的按钮
 */
- (void)addTitlesBtn
{
    NSArray *titles = @[@"科目一",@"科目二",@"科目三",@"科目四"];
    static NSUInteger count = 4;
    for (NSUInteger i = 0;  i < count; i++) {
        YCExamBeforeTitleButton *btn = [YCExamBeforeTitleButton buttonWithType:UIButtonTypeCustom];
        [btn setTitle:titles[i] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        CGFloat btnH = 49;
        CGFloat btnW = self.titlesView.yc_width / count;
        CGFloat btnX = i * btnW;
        CGFloat btnY = 0;
        btn.frame = CGRectMake(btnX, btnY, btnW, btnH);
        [btn addTarget:self action:@selector(titleBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.titlesView addSubview:btn];
        
    }
    
}
/**
 *  添加下划线
 */
- (void)addUnderLine
{
    YCExamBeforeTitleButton *fistBtn = [self.titlesView.subviews firstObject];
    UIView *underline = [[UIView alloc] init];
    CGFloat underlineH = 2;
    CGFloat underlineW = self.titlesView.yc_width / 4.0;
    CGFloat underlineX = 0;
    CGFloat underlineY = self.titlesView.yc_height - underlineH;
    underline.frame = CGRectMake(underlineX, underlineY, underlineW, underlineH);
    //设置下划线的颜色,根随按钮选中的颜色一致
    underline.backgroundColor = [fistBtn titleColorForState:UIControlStateSelected];
    [self.titlesView addSubview:underline];
    _underline = underline;
    
    //选中第一个按钮
    fistBtn.selected = YES;
    self.currentSelectedBtn = fistBtn;
    [fistBtn.titleLabel sizeToFit];
    self.underline.yc_centerX = fistBtn.yc_centerX;
    // 设置下划线宽度
    //    self.underline.width = btn.titleLabel.width + 20;
}

/**
 *  添加所有的子控制器
 */
- (void)setUpChildVcs
{
    //科目一
    YCCourseOneViewController *courseOneVC = [[YCCourseOneViewController alloc] init];
    [self addChildViewController:courseOneVC];
    
    //科目二
    YCCourseTwoViewController *courseTwoVC = [[YCCourseTwoViewController alloc] init];
    [self addChildViewController:courseTwoVC];
    
    //科目三
    YCCourseThreeViewController *courseThreeVC = [[YCCourseThreeViewController alloc] init];
    [self addChildViewController:courseThreeVC];
    
    //科目四
    YCCourseFourViewController *courseFourVC = [[YCCourseFourViewController alloc] init];
    [self addChildViewController:courseFourVC];
    
    //设置scrollView中contentSize
    NSInteger count = self.childViewControllers.count;
    _scrollView.contentSize = CGSizeMake(count * _scrollView.yc_width, 0);
    
}

#pragma mark - 监听


/**
 *  点击了某个标题按钮
 */
- (void)titleBtnClick:(YCExamBeforeTitleButton *)btn
{
    self.currentSelectedBtn.selected = NO;
    btn.selected = YES;
    self.currentSelectedBtn = btn;
    NSInteger index = [self.titlesView.subviews indexOfObject:btn];
    
    //下划线移动下选中的按钮下
    [UIView animateWithDuration:0.2 animations:^{
        self.underline.yc_centerX = btn.yc_centerX;
    } completion:^(BOOL finished) {
        [self addChildVcViewToScrollView:index];
    }];
    
}


#pragma mark - UIScrollViewDelegate

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    CGFloat offsetX = scrollView.contentOffset.x;
    NSInteger index = offsetX / self.scrollView.yc_width;
    //选中那个标题
    [self titleBtnClick:self.titlesView.subviews[index]];
}
/**
 *  添加对应索引的控制器view到scrollView中
 */
- (void)addChildVcViewToScrollView:(NSInteger)index
{
    UIViewController *VC = self.childViewControllers[index];
    if (![VC isViewLoaded]) {
        VC.view.backgroundColor = [UIColor yc_colorWithUInt:0xf7f7f7 alpha:1.0];
        VC.view.frame = CGRectMake(index * self.scrollView.yc_width, 0, self.scrollView.yc_width, self.scrollView.yc_height);
        [self.scrollView addSubview:VC.view];
    }
    [self.scrollView setContentOffset:CGPointMake(index * self.scrollView.yc_width, 0) animated:NO];
    
}




@end
